package com.shaoshaohuo.app.view;

import android.app.Dialog;
import android.content.Context;
import android.os.Handler;
import android.support.annotation.NonNull;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.TextView;

import com.shaoshaohuo.app.R;

/**
 * Created by Administrator on 2017/8/17.
 */

public class UpdateDialog extends Dialog implements View.OnClickListener {

    private Handler handler = new Handler();
    private callBack mcallBack;

    public void setListener(callBack m) {
        this.mcallBack = m;
    }

    public UpdateDialog(@NonNull Context context, String da, String value) {
        super(context, R.style.coudiast);
        this.url = da;
        this.value = value;
        init();

    }

    private String value;
    private static int REQUESTPERMISSION = 110;

    private void init() {
        View inflate = LayoutInflater.from(getContext()).inflate(R.layout.update_layout, null);
        setContentView(inflate);
        WindowManager.LayoutParams attributes = getWindow().getAttributes();
        attributes.dimAmount = 0.8f;
        attributes.alpha = 1f;
        attributes.width = (int) (getContext().getResources().getDisplayMetrics().widthPixels * (3f / 4f));
        attributes.height = (int) (getContext().getResources().getDisplayMetrics().heightPixels / 2 - getstarbarheight());
        setCanceledOnTouchOutside(true);
        inflate.findViewById(R.id.btn_up).setOnClickListener(this);
        TextView tv_value_up = (TextView) inflate.findViewById(R.id.tv_value_up);
        tv_value_up.setText(value);
    }

    private String url;

    @Override
    public void onClick(View v) {
        if (mcallBack != null) {
            Log.e("TAG", "onClick: " + 1111111111);
            dismiss();
            mcallBack.backListener(url);
        }
//        dismiss();
//        VersionUpdateConfig.getInstance()//获取配置实例
//                .setContext(getContext())//设置上下文
//                .setDownLoadURL(url)//设置文件下载链接
////                                .setFileSavePath(savePath)//设置文件保存路径（可不设置）
//                .setNotificationIconRes(R.mipmap.logo)//设置通知图标
//                .setNotificationSmallIconRes(R.mipmap.logo)//设置通知小图标
//                .setNotificationTitle("版本升级")//设置通知标题
//                .startDownLoad();//开始下载
    }

    public float getstarbarheight() {
        try {
            Class<?> aClass = Class.forName("android.R.dimen.status_bar_height");
            Object o = aClass.newInstance();
            String status_bar_height = aClass.getField("status_bar_height").get(0).toString();
            int i = Integer.parseInt(status_bar_height);
            float dimension = getContext().getResources().getDimension(i);
            return dimension;
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        }
        return -1;
    }

    public interface callBack {
        public void backListener(String url);
    }
}
